/**
* dbsvr 定义
* @author thinkry
*/

#ifndef __DBSVR_DEF_H__
#define __DBSVR_DEF_H__

#include "CTBase.h"
#include "CTBuffer.h"
#include "CTStat.h"
#include "CTSizeString.h"
#include "CTArray.h"
#include "CTObjectMgr.h"
#include "CTHashObjectMgr.h"
#include "CTShmQueue.h"
#include "net/CTEpoll.h"
#include "CTMsgBase.h"
#include "CTConfig.h"
#include "CTServerUtils.h"
#include "CTAsyncCmd.h"
#include "CTAsyncCmdFactory.h"
#include "CTCodeMySQL.h"
#include "commmsg.h"
#include "commclassdb.h"

#include <string>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;
using namespace CTLib;

//===============================================
// 相关文件的定义，做为一个约定，不用在配置文件中读取

#define APPNAME             "dbsvr"
#define FILE_CONFIG         "../config/" APPNAME ".conf"
#define FILE_NORMALLOG      "../log/" APPNAME ".log"
#define FILE_ERRORLOG       "../log/" APPNAME ".err"
#define FILE_STATLOG        "../log/" APPNAME ".sta"
#define FILE_PIDFILE        "../bin/" APPNAME ".pid"
#define FILE_UPDATELOG        "../log/" APPNAME ".upt"

#define CHECK_RETURN(iRet)  do { if (iRet) { return iRet; } } while(0)
#define CHECK_EXIT(iRet) do { if (iRet) { exit(iRet); } } while(0)
#define CHECK_THROW(iRet) do { if (iRet) { CT_THROW((iRet)); } } while(0)

//===============================================
// 这两个定义是为了能够在Windows下编译
#ifdef WIN32
    #define signal(signum, handler) (0)
    #define getopt(argc, argv, optstring) (0)

    #define SIGUSR1 10
    #define SIGUSR2 12
#else
    #include <sys/types.h>
    #include <signal.h>
#endif

//===============================================
enum db_const
{
    DB_EVENT_SIZE =		64, ///< epoll参数
    DB_WAIT_SIZE  =		64, ///< epoll参数
    MAX_IPLEN = 32,
    MAX_TRACE_SQL =     300, ///< 缓存的trace sql数量，用于批量写入文件

#ifdef _DEBUG
    HASH_UPDATE_INTERVAL = 5,        //hash更新间隔（秒）
    MAX_HASH_UPDATE_COUNT = 10,       //每次更新最大数量
#else
    HASH_UPDATE_INTERVAL = 5,         //hash更新间隔（秒）
    MAX_HASH_UPDATE_COUNT = 300,           //每次更新最大数量
#endif
    MAX_HASH_USE_PERCENT = 90, 
    MAX_HASH_USE_TOTAL_PERCENT = 100, //当非全局类hash使用比例大于(MAX_HASH_USE_PERCENT / MAX_HASH_USE_TOTAL_PERCENT)需做唤出操作
    MAX_HASH_ALARM = 5,               //当达到（MAX_HASH_ALARM * HASH_MAX_SIZE / MAX_HASH_USE_TOTAL_PERCENT）时，不再接受前台请求
    MAX_DBNAME_LEN = 50,
};

//===============================================
// 统计项的定义

/**
* 异步命令的Do参数
*/
class CCmdDoParam
{
public:
    void*               m_pMain;
    CEnpNetHead*        m_pstHead;
    CTMsgHead*          m_pstMsgHead;
    CTCodeStream*       m_pstCode;

    CCmdDoParam(void* pMain, CEnpNetHead& stHead, CTMsgHead& stMsgHead, CTCodeStream& stCode) :
    m_pMain(pMain),
        m_pstHead(&stHead),
        m_pstMsgHead(&stMsgHead),
        m_pstCode(&stCode)
    {   
        CT_ASSERT(m_pMain);
    }

private:
    // 禁止赋值
    CCmdDoParam& operator = (const CCmdDoParam& obj)
    {   
        return *this;    
    }
};

/**
* 异步命令的Answer参数
*/
class CCmdOnAnswerParam
{
public:
    void*               m_pMain;
    CTMsgHead*          m_pstMsgHead;
    CTCodeStream*       m_pstCode;

    CCmdOnAnswerParam(void* pMain, CTMsgHead& stMsgHead, CTCodeStream& stCode) :
    m_pMain(pMain),
        m_pstMsgHead(&stMsgHead),
        m_pstCode(&stCode)
    {
        CT_ASSERT(m_pMain);
    }
private:
    // 禁止赋值
    CCmdOnAnswerParam& operator = (const CCmdOnAnswerParam& obj)
    {
        return *this;
    }
};

// 统计的CTLog
#define CT_STAT (CTLib::CTSingleton<CTLib::CTLog, 2>::Instance())

#define CT_UPT   (CTLib::CTSingleton<CTLib::CTLog, 3>::Instance())

//更新日志
#ifndef _DEBUG
#define CT_UPDATE(x) do { \
    ; } while(0)
#else
#define CT_UPDATE(x) do { \
    CT_UPT->LogTrace x; } while(0)
#endif


typedef struct CTraceSql
{
    int iSwapCount;
    CReqSQL stReqSql;

    CTraceSql()
        :iSwapCount(0)
    {
        
    }
}CTraceSql;

/**
* 统计项.
* 必须以0开始顺序编号, 方便和描述的下标对应
*/
enum EMainStat
{
    db_stat_recvmsg = 0,
    db_stat_sendmsg,
    db_stat_accept,
    db_stat_idleclose,
    db_stat_mainloop,
    db_stat_waittimeout,
    db_stat_check,
};

#endif //__DBSVR_DEF_H__
